package com.dks.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dks.dto.DiaryDto;
import com.dks.mapper.DiaryMapper;
import com.dks.mapper.PublicAttentionMapper;
import com.dks.models.Diary;
import com.dks.models.publicCompoment.PublicAttention;
import com.dks.repository.IDiaryRepository;
import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;
import com.dks.util.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class DiaryRepository implements IDiaryRepository {

    private DiaryMapper diaryMapper;

    @Autowired
    private PublicAttentionMapper publicAttentionMapper;

    @Autowired
    public DiaryRepository(final DiaryMapper diaryMapperIn) {
        this.diaryMapper = diaryMapperIn;
    }

    @Override
    public Integer insert(Diary diary) {
        return diaryMapper.insert(diary);
    }

    @Override
    public Integer update(Diary diary) {
        return diaryMapper.update(diary);
    }

    @Override
    public Integer delete(Integer id) {
        return diaryMapper.delete(id);
    }

    @Override
    public Diary select(Integer id) {
        return diaryMapper.select(id);
    }

    @Override
    public Integer cntByMap(Map<String, Object> map) {
        return diaryMapper.cntByMap(map);
    }

    @Override
    public List<Integer> getByMap(Map<String, Object> map) {
        return diaryMapper.getByMap(map);
    }

    @Override
    public List<Diary> getByMapList(Map<String, Object> map) {
        return diaryMapper.getByMapList(map);
    }

    @Override
    public Integer updateByCode(Diary diary) {
        return diaryMapper.updateByCode(diary);
    }

    @Override
    public Diary selectByBfCode(String bfCode) {
        return diaryMapper.selectByBfCode(bfCode);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取教师日记列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: teacherId 老师id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getCourseTeacherList(Integer teacherId, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("teacherId", teacherId);
        map.put("recStatus", 0);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<Diary> diaryList = diaryMapper.getByMapList(map);
        PageInfo<Diary> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), diaryList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }


    /**
     * @Author: Ah Nan
     * @Description: 获取教师日记详情
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: diaryId 日记id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    @Transactional
    public ResultModel<Object> getCourseTeacherInfo(Integer diaryId, Integer userId) {
        Diary diary = diaryMapper.selectDiary(diaryId);

        //获取详情后浏览量+1
        if (null != diary) {
            int row = diaryMapper.updateBrowseCount(Long.valueOf(diaryId));
        }
        DiaryDto diaryDto = new DiaryDto();
        BeanUtils.copyProperties(diary, diaryDto);
        //查询当前用户是否关注了老师
        PublicAttention publicAttention = publicAttentionMapper.select("TEACHER", diary.getTeacherId(), userId);
        if (null == publicAttention) {
            diaryDto.setUserAttensionTeacherStatus(false);
        } else {
            diaryDto.setUserAttensionTeacherStatus(true);
        }
        return Result.makeResult(ResultInfo.SUCCESS, diaryDto);
    }

    /**
     * @Author: Ah Nan
     * @Description: 发布日记
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: diaryId 日记id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> addTeacherDiary(Diary diary) {
        diary.setCreateUserId(diary.getTeacherId());
        diary.setModifyUserId(diary.getTeacherId());
        int row = diaryMapper.insert(diary);
        return Result.makeResult(ResultInfo.SUCCESS, row);

    }


}
